package com.gavin.plugin.database;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class DataEngineList {
	private ArrayList<DataEngine> alDataEngine = new ArrayList<DataEngine>();
	private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

	public void add(DataEngine dataEngine) {
		this.alDataEngine.add(dataEngine);
	}

	public DataEngine get(int nIndex) {
		DataEngine dataEngine = null;
		lock.readLock().lock();
		try {
			dataEngine = this.alDataEngine.get(nIndex);
		} catch (Exception e) {
			return null;
		} finally {
			lock.readLock().unlock();
		}
		return dataEngine;
	}

	public DataEngine get() {
		return this.get(0);
	}

	public void clear() {
		lock.writeLock().lock();
		this.alDataEngine.clear();
		lock.writeLock().unlock();
	}

	public int size() {
		lock.readLock().lock();
		int nSize = this.alDataEngine.size();
		lock.readLock().unlock();
		return nSize;
	}
}
